package com.wswy.wzcx.ui.adapter;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTFeedAd;
import com.bytedance.sdk.openadsdk.TTImage;
import com.bytedance.sdk.openadsdk.TTNativeAd;
import com.bytedance.sdk.openadsdk.TTNativeAd.AdInteractionListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.wswy.wzcx.BuildConfig;
import com.wswy.wzcx.R;
import com.wswy.wzcx.utils.ImageUtils;
import java.util.ArrayList;
import java.util.List;

class TTADViewHolders {

  private static final int ITEM_VIEW_TYPE_LARGE_PIC_AD = 23;
  private static final int ITEM_VIEW_TYPE_GROUP_PIC_AD = 24;
  private static final int ITEM_VIEW_TYPE_SMALL_PIC_AD = 25;
  private static final int ITEM_VIEW_TYPE_VIDEO = 26;


  static int getViewType(TTFeedAd ad,int defType){
    int type;
    switch (ad.getImageMode()) {

      case TTAdConstant.IMAGE_MODE_LARGE_IMG:
        type = ITEM_VIEW_TYPE_LARGE_PIC_AD;
        break;
      case TTAdConstant.IMAGE_MODE_GROUP_IMG:
        type = ITEM_VIEW_TYPE_GROUP_PIC_AD;
        break;
      case TTAdConstant.IMAGE_MODE_SMALL_IMG:
        type = ITEM_VIEW_TYPE_SMALL_PIC_AD;
        break;
      case TTAdConstant.IMAGE_MODE_VIDEO:
        type = ITEM_VIEW_TYPE_VIDEO ;
        break;
      default:
        type = defType;
    }

    return type;
  }

  static boolean isTTADType(int viewType){
    return viewType >= ITEM_VIEW_TYPE_LARGE_PIC_AD && viewType <= ITEM_VIEW_TYPE_VIDEO;
  }

  static boolean isTTADViewHolder(RecyclerView.ViewHolder holder){
    return holder instanceof TTADViewHolder;
  }

  static ViewHolder createViewHolder(@NonNull ViewGroup parent, int viewType){
    Context context = parent.getContext();
    switch (viewType){
      case ITEM_VIEW_TYPE_LARGE_PIC_AD:
        return new LargeAdViewHolder(LayoutInflater.from(context).inflate(R.layout.ttad_listitem_large_pic, parent, false));
      case ITEM_VIEW_TYPE_SMALL_PIC_AD:
        return new SmallAdViewHolder(LayoutInflater.from(context).inflate(R.layout.ttad_listitem_small_pic, parent, false));
      case ITEM_VIEW_TYPE_GROUP_PIC_AD:
        return new GroupAdViewHolder(LayoutInflater.from(context).inflate(R.layout.ttad_listitem_group_pic, parent, false));
      case ITEM_VIEW_TYPE_VIDEO:
        return new LargeVideoViewHolder(LayoutInflater.from(context).inflate(R.layout.ttad_listitem_large_video, parent, false));
    }
    return null;
  }


  static void bindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position,TTFeedAd ttFeedAd,View.OnClickListener closeAdClickListener){

    if (holder instanceof TTADViewHolder){
      TTADViewHolder adViewHolder = (TTADViewHolder) holder;


      adViewHolder.mTitle.setText(ttFeedAd.getDescription());
      adViewHolder.mSource.setText(TextUtils.isEmpty(ttFeedAd.getSource()) ? ttFeedAd.getTitle() : ttFeedAd.getSource());

      adViewHolder.adClose.setTag(R.id.item_position,position);
      adViewHolder.adClose.setTag(R.id.item_data,ttFeedAd);
      adViewHolder.adClose.setOnClickListener(closeAdClickListener);


      //可以被点击的view, 也可以把convertView放进来意味item可被点击
      List<View> clickViewList = new ArrayList<View>(adViewHolder.getClickView());
      //触发创意广告的view（点击下载或拨打电话）
      List<View> creativeViewList = new ArrayList<View>(adViewHolder.getClickView());

      //重要! 这个涉及到广告计费，必须正确调用。convertView必须使用ViewGroup。

      ttFeedAd.registerViewForInteraction((ViewGroup) adViewHolder.itemView, clickViewList, creativeViewList,adViewHolder.adClose, new ADListener(ttFeedAd));

    }

    if (holder instanceof LargeAdViewHolder) {

      LargeAdViewHolder adViewHolder = (LargeAdViewHolder) holder;

      List<TTImage> imageList = ttFeedAd.getImageList();
      if (imageList != null && !imageList.isEmpty()) {
        TTImage image = imageList.get(0);
        if (image != null && image.isValid()) {
          ImageUtils.showImage(adViewHolder.mIcon,image.getImageUrl());
        }
      }
    }else if (holder instanceof SmallAdViewHolder){

      SmallAdViewHolder adViewHolder = (SmallAdViewHolder) holder;

      List<TTImage> imageList = ttFeedAd.getImageList();
      if (imageList != null && !imageList.isEmpty()) {
        TTImage image = imageList.get(0);
        if (image != null && image.isValid()) {
          ImageUtils.showImage(adViewHolder.mIcon,image.getImageUrl());
        }
      }


    }else if (holder instanceof GroupAdViewHolder){


      GroupAdViewHolder adViewHolder = (GroupAdViewHolder) holder;
      List<TTImage> imageList = ttFeedAd.getImageList();

      if (imageList != null && imageList.size() >=3) {

        TTImage image1 = imageList.get(0);
        TTImage image2 = imageList.get(1);
        TTImage image3 = imageList.get(2);
        if (image1 != null && image1.isValid()) {
          ImageUtils.showImage(adViewHolder.mIcon1, image1.getImageUrl());
        }
        if (image2 != null && image2.isValid()) {
          ImageUtils.showImage(adViewHolder.mIcon2, image2.getImageUrl());
        }
        if (image3 != null && image3.isValid()) {

          ImageUtils.showImage(adViewHolder.mIcon3, image3.getImageUrl());

        }

      }
    }else if (holder instanceof LargeVideoViewHolder){

      View adView = ttFeedAd.getAdView();


      ((LargeVideoViewHolder) holder).mVideoContainer.removeAllViews();

      if (adView != null){

        if (adView.getParent() instanceof ViewGroup) {

          ((ViewGroup) adView.getParent()).removeView(adView);
        }

        ((LargeVideoViewHolder) holder).mVideoContainer.addView(adView);

      }

    }



  }



  static class TTADViewHolder extends RecyclerView.ViewHolder{

    TextView mTitle;
    TextView mSource;
    View adClose;

    TTADViewHolder(View itemView) {
      super(itemView);

      mTitle = itemView.findViewById(R.id.tv_ad_title);
      mSource = itemView.findViewById(R.id.tv_ad_source);
      adClose = itemView.findViewById(R.id.img_ad_close);
    }

    List<View> getClickView(){
      return null;
    }


  }

  static class LargeAdViewHolder extends TTADViewHolder {
    SimpleDraweeView mIcon;

    private List<View> clickViews = new ArrayList<>(1);

    LargeAdViewHolder(View itemView) {
      super(itemView);
      mIcon = itemView.findViewById(R.id.iv_listitem_image);
      clickViews.add(mTitle);
      clickViews.add(mIcon);
      clickViews.add(mSource);
    }

    @Override
    List<View> getClickView() {
      return clickViews;
    }
  }

  static class LargeVideoViewHolder extends TTADViewHolder {
    FrameLayout mVideoContainer;

    private List<View> clickViews = new ArrayList<>(1);

    LargeVideoViewHolder(View itemView) {
      super(itemView);
      mVideoContainer = itemView.findViewById(R.id.fl_listitem_video);
      clickViews.add(mTitle);
      clickViews.add(mVideoContainer);
      clickViews.add(mSource);
    }

    @Override
    List<View> getClickView() {
      return clickViews;
    }
  }


  private static class SmallAdViewHolder extends TTADViewHolder{
    SimpleDraweeView mIcon;

    private List<View> clickViews = new ArrayList<>(3);

    public SmallAdViewHolder(@NonNull View itemView) {
      super(itemView);
      mIcon = itemView.findViewById(R.id.iv_listitem_image);

      clickViews.add(mIcon);
      clickViews.add(mTitle);
      clickViews.add(mSource);
    }

    @Override
    List<View> getClickView() {
      return clickViews;
    }
  }


  private static class GroupAdViewHolder extends TTADViewHolder{
    SimpleDraweeView mIcon1;
    SimpleDraweeView mIcon2;
    SimpleDraweeView mIcon3;

    private List<View> clickViews = new ArrayList<>(4);

    GroupAdViewHolder(@NonNull View itemView) {
      super(itemView);
      mIcon1 = itemView.findViewById(R.id.iv_listitem_image_1);
      mIcon2 = itemView.findViewById(R.id.iv_listitem_image_2);
      mIcon3 = itemView.findViewById(R.id.iv_listitem_image_3);

      clickViews.add(mTitle);
      clickViews.add(mIcon1);
      clickViews.add(mIcon2);
      clickViews.add(mIcon3);
      clickViews.add(mSource);
    }

    @Override
    List<View> getClickView() {
      return clickViews;
    }
  }




  private static class ADListener implements AdInteractionListener {
    TTFeedAd ttFeedAd;
    ADListener(TTFeedAd ttFeedAd){
      this.ttFeedAd = ttFeedAd;
    }

    @Override
    public void onAdClicked(View view, TTNativeAd ttNativeAd) {
      if (BuildConfig.DEBUG) {
        Log.e("test", "onAdClicked --> " + "广告 " + ttFeedAd.getTitle() + "被点击");
      }

    }

    @Override
    public void onAdCreativeClick(View view, TTNativeAd ttNativeAd) {
      if (BuildConfig.DEBUG) {
        Log.e("test", "onAdCreativeClick --> " + "广告 " + ttFeedAd.getTitle() + "被创意按钮被点击");
      }

    }

    @Override
    public void onAdShow(TTNativeAd ttNativeAd) {
      if (BuildConfig.DEBUG) {
        Log.e("test", "onAdShow --> " + "广告 " + ttFeedAd.getTitle() + "展示");
      }

    }


  };
}
